#region Copyright
// Copyright (c) 2010, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>.
// This source is subject to the Microsoft Public License. 
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 
// All other rights reserved.
#endregion

namespace LiveWriter.Plugins
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    internal static class ComboBoxExtensions
    {
        public static void SetSelectedIndex(this ComboBox instance, string selectedValue)
        {
            if (string.IsNullOrEmpty(selectedValue))
            {
                return;
            }

            for (int i = 0; i < instance.Items.Count; i++)
            {
                Pair pair = (Pair)instance.Items[i];

                if (pair.Key.Equals(selectedValue))
                {
                    instance.SelectedIndex = i;
                    break;
                }
            }
        }

        public static void Load<T>(this ComboBox target, IEnumerable<T> items)
        {
            target.Items.Clear();
            target.Items.AddRange(items.Cast<object>().ToArray());
            target.SelectedIndex = 0;
        }
    }
}